package top.zhouze.common.util;

import android.annotation.SuppressLint;
import android.content.Context;
import lombok.SneakyThrows;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 周仄
 * @since 2023/2/2 16:56
 */
public class StrUtil {

    public static boolean isBlank(String str) {
        if (str == null) {
            return true;
        }
        return str.trim().isEmpty();
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static Matcher matcher(String regexp, String value) {
        return Pattern.compile(regexp).matcher(value);
    }

    public static boolean isMatching(String regexp, String value) {
        if (isEmpty(value)) {
            return false;
        }
        return Pattern.matches(regexp, value);
    }

    public static boolean isFind(String regexp, String value) {
        if (isEmpty(value)) {
            return false;
        }
        Pattern pattern = Pattern.compile(regexp);
        return pattern.matcher(value).find();
    }

    @SuppressLint("SimpleDateFormat")
    public static String getNowTime(String format) {
        return new SimpleDateFormat(format).format(new Date());
    }

    public static String getNowTime() {
        return getNowTime("yyyy-MM-dd HH:mm:ss");
    }


    /**
     * 驼峰转下划线 snakeCase -> snake_case
     *
     * @param str str
     * @return {@link String}
     */
    public static String humpToUnderline(String str) {
        return str.replaceAll("([a-z])([A-Z])","$1_$2").toLowerCase();
    }

    /**
     * 下划线转驼峰 camel_case -> camelCase
     *
     * @param str str
     * @return {@link String}
     */
    public static String underlineToHump(String str) {
        Matcher matcher = Pattern.compile("_([a-z])").matcher(str);
        matcher.reset();
        boolean result = matcher.find();
        if (result) {
            StringBuffer sb = new StringBuffer();
            do {
                matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
                result = matcher.find();
            } while (result);
            matcher.appendTail(sb);
            return sb.toString();
        }
        return str;
    }

    /**
     * 将字符串转成MD5值
     * @param string 需要转换的字符串
     * @return 字符串的MD5值
     */
    @SneakyThrows
    public static String strToMD5(String string) {
        byte[] hash = MessageDigest.getInstance("MD5").digest(string.getBytes(StandardCharsets.UTF_8));
        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) {
                hex.append("0");
            }
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }

    /**
     * 字节数组转为字符串
     *
     * @param bytes 字节
     * @return {@link String}
     */
    public static String byteArrayToString(byte[] bytes) {
        char[] chars = new char[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            chars[i] = (char) bytes[i];
        }
        return String.valueOf(chars);
    }

    /**
     * 加载字符串
     * @param id 字符串id
     * */
    public static String getStaticString(Context context, int id){
        return context.getResources().getString(id);
    }
}
